'use strict';

const path = require('path');
const HtmlPlugin = require('html-webpack-plugin');
const LodashPlugin = require('lodash-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
    mode: 'development',
    entry: {
        dev: './dev/index.js'
    },
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, '../dist/dev')
    },

    devtool: 'cheap-module-source-map',

    plugins: [
        new HtmlPlugin({
            template: './dev/index.html'
        }),
        new LodashPlugin(),
        new MiniCssExtractPlugin({
            filename: 'query-builder.css'
        }),
    ],

    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        "presets": [
                            [
                                "@babel/preset-env",
                                {
                                    "targets": "defaults"
                                }
                            ],
                            "@babel/preset-react"
                        ],
                        "plugins": [
                            "lodash"
                        ]
                    }
                },
                exclude: /node_modules/,
            },
            {
                test: /\.s[ac]ss$/i,
                use: [
                    MiniCssExtractPlugin.loader,
                    "css-loader",
                    {
                        loader: "sass-loader",
                        options: {
                            // Prefer `dart-sass`,not use node-sass
                            implementation: require("sass"),
                        },
                    },
                ],
            }
        ],
    },
    resolve: {
        extensions: ['.js', '.jsx', '.scss']
    },

    optimization: {
        splitChunks: {
            chunks: 'all',
            minSize: 10000,
            maxSize: 0,
            minChunks: 1,
            maxAsyncRequests: 5,
            maxInitialRequests: 4,
            automaticNameDelimiter: '~',
            name: true,
            cacheGroups: {
                react: {
                    test: /[\\/]node_modules[\\/](react|react-dom)[\\/]/,
                    name: 'react'
                },
                lodash: {
                    test: /[\\/]node_modules[\\/](lodash)[\\/]/,
                    name: 'lodash'
                },
                others: {
                    test: /[\\/]node_modules[\\/]/,
                    name: 'others',
                    priority: -10
                },
            }
        }
    }
};